﻿namespace NTracker.Tests.Configs
{
    using NTracker.Configs;
    using NUnit.Framework;
    using Rhino.Mocks;
    using System.Xml.Linq;
    using System;
    using System.IO;
    using System.Collections.Generic;

    [TestFixture]
    public class ConfigResourcesTests
    {
        private ConfigResources resource;
        private MockRepository mockRepository;
        private ISettingsManager mockSettings;
        private Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject wrapper;

        [TestFixtureSetUp]
        public void SetUp()
        {
            mockRepository = new MockRepository();
            mockSettings = mockRepository.DynamicMock<ISettingsManager>();
            using (mockRepository.Record())
            {
                mockSettings.GetSettings();
                LastCall.Return(
                    new XElement("nt-config",
                        new XElement("core",
                            new XElement("libraries",
                                new XElement("import", new XAttribute("id", "dll"), new XAttribute("value", "*.dll")),
                                new XElement("import", new XAttribute("id", "exe"), new XAttribute("value", "*.exe"))
                            ),
                            new XElement("directories",
                                new XElement("path", new XAttribute("id", "default"), new XAttribute("value", "$(CurrentDirectory)\\Modules")),
                                new XElement("path", new XAttribute("id", "current"), new XAttribute("value", "$(CurrentDirectory)"))
                            )
                        )
                    )
                );
            }

            resource = new ConfigResources(mockSettings);
            wrapper = new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(resource);
        }

        [Test]
        public void ConfigureDynamicLanguage_WhenStringContainsSpecialWords_ReturnFilteredString()
        {
            // Given
            string actual = string.Empty;
            string expected = Directory.GetCurrentDirectory();

            // When
            actual = Convert.ToString(wrapper.Invoke("FilterDynamicXmlLanguage", "$(CurrentDirectory)"));

            // Then
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void ConfigureDynamicLanguage_WhenStringNotContainsSpecialWords_ReturnFilteredString()
        {
            // Given
            string actual = string.Empty;
            string expected = "mock_the_life";

            // When
            actual = Convert.ToString(wrapper.Invoke("FilterDynamicXmlLanguage", "mock_the_life"));

            // Then
            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void PopulateSettings_WhenExistingSettings()
        {
            // Given
            Dictionary<string, string>[] actual;
            Dictionary<string, string> expectedLibraries = new Dictionary<string, string>
            {
                {"dll", "*.dll"},
                {"exe", "*.exe"}
            };

            Dictionary<string, string> expectedDirectories = new Dictionary<string, string>
            {
                {"default", Directory.GetCurrentDirectory() + "\\Modules" },
                {"current", Directory.GetCurrentDirectory() }
            };

            // When
            actual = (Dictionary<string, string>[])wrapper.Invoke("PopulateSettings");
            
            // Then
            Assert.IsNotNull(actual);
            Assert.AreEqual(expectedDirectories, actual[C.DIRECTORY]);
            Assert.AreEqual(expectedLibraries, actual[C.LIBRARY]);
        }
    }
}
